package com.seedog.mq.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息属性类，存储消息的附加信息
 */
public class MessageProperties implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 消息头属性
    private Map<String, String> headers;
    // 消息的优先级
    private int priority = 5;
    // 是否持久化
    private boolean persistent = true;
    // 消息重试次数
    private int retryCount = 0;
    // 消息来源
    private String source;
    // 消息目标
    private String target;
    
    public MessageProperties() {
        this.headers = new HashMap<>();
    }
    
    // 获取指定名称的头部属性
    public String getHeader(String key) {
        return headers.get(key);
    }
    
    // 设置头部属性
    public void setHeader(String key, String value) {
        headers.put(key, value);
    }
    
    // 获取所有头部属性
    public Map<String, String> getHeaders() {
        return new HashMap<>(headers);
    }
    
    // 设置头部属性映射
    public void setHeaders(Map<String, String> headers) {
        this.headers.clear();
        if (headers != null) {
            this.headers.putAll(headers);
        }
    }
    
    // 获取优先级
    public int getPriority() {
        return priority;
    }
    
    // 设置优先级（0-9，数字越大优先级越高）
    public void setPriority(int priority) {
        if (priority >= 0 && priority <= 9) {
            this.priority = priority;
        } else {
            throw new IllegalArgumentException("Priority must be between 0 and 9");
        }
    }
    
    // 是否持久化
    public boolean isPersistent() {
        return persistent;
    }
    
    // 设置是否持久化
    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }
    
    // 获取重试次数
    public int getRetryCount() {
        return retryCount;
    }
    
    // 设置重试次数
    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }
    
    // 增加重试次数
    public void incrementRetryCount() {
        this.retryCount++;
    }
    
    // 获取消息来源
    public String getSource() {
        return source;
    }
    
    // 设置消息来源
    public void setSource(String source) {
        this.source = source;
    }
    
    // 获取消息目标
    public String getTarget() {
        return target;
    }
    
    // 设置消息目标
    public void setTarget(String target) {
        this.target = target;
    }
    
    @Override
    public String toString() {
        return "MessageProperties{" +
                "headers=" + headers +
                ", priority=" + priority +
                ", persistent=" + persistent +
                ", retryCount=" + retryCount +
                ", source='" + source + '\'' +
                ", target='" + target + '\'' +
                '}';
    }
}